package com.ruoyi.business.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.POJO.CopyAcAssetInfoPOJO;
import com.ruoyi.business.VO.AcAssetInfoVO;
import com.ruoyi.business.VO.AcAssetLeaseInfoVO;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.AcWorkOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.AcAssetInfo;
import com.ruoyi.business.service.IAcAssetInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资产台账管理Controller
 *
 * @author ruoyi
 * @date 2025-01-15
 */
//@EnableSwagger2
@RestController
//@Api("资产信息")
@RequestMapping("/business/assetInfo")
public class AcAssetInfoController extends BaseController
{
    @Autowired
    private IAcAssetInfoService acAssetInfoService;



    /**
     * 查询资产台账管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:assetInfo:list')")
    @GetMapping("/list")
    @ApiOperation("查询资产台账管理列表")
    public TableDataInfo list(AcAssetInfo acAssetInfo)
    {
        startPage();
        List<AcAssetInfo> list = acAssetInfoService.selectAcAssetInfoList(acAssetInfo);
        return getDataTable(list);
    }

    /**
     * 导出资产台账管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:assetInfo:export')")
    @Log(title = "资产台账管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出资产台账管理列表")
    public void export(HttpServletResponse response, AcAssetInfo acAssetInfo)
    {
        List<AcAssetInfo> list = acAssetInfoService.selectAcAssetInfoList(acAssetInfo);
        ExcelUtil<AcAssetInfo> util = new ExcelUtil<AcAssetInfo>(AcAssetInfo.class);
        util.exportExcel(response, list, "资产台账管理数据");
    }

    /**
     * 获取资产台账管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:assetInfo:query')")
    @GetMapping(value = "/{assetId}")
    @ApiOperation("获取资产台账管理详细信息")
    public AjaxResult getInfo(@PathVariable("assetId") Long assetId)
    {
        return success(acAssetInfoService.selectAcAssetInfoByAssetId(assetId));
    }

    /**
     * 新增资产台账管理
     */
    @PreAuthorize("@ss.hasPermi('business:assetInfo:add')")
    @Log(title = "资产台账管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增资产台账管理")
    public AjaxResult add(@RequestBody AcAssetInfo acAssetInfo) {
        return toAjax(acAssetInfoService.insertAcAssetInfo(acAssetInfo));
    }

    /**
     * 修改资产台账管理
     */
    @PreAuthorize("@ss.hasPermi('business:assetInfo:edit')")
    @Log(title = "资产台账管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改资产台账管理")
    public AjaxResult edit(@RequestBody AcAssetInfo acAssetInfo)
    {
        return toAjax(acAssetInfoService.updateAcAssetInfo(acAssetInfo));
    }

    /**
     * 删除资产台账管理
     */
    @PreAuthorize("@ss.hasPermi('business:assetInfo:remove')")
    @Log(title = "资产台账管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{assetIds}")
    @ApiOperation("删除资产台账管理")
    public AjaxResult remove(@PathVariable Long[] assetIds)
    {
        int deleteAssetIsOk = acAssetInfoService.checkAndDeleteAcAssetInfo(assetIds);
        if(0 == deleteAssetIsOk) {
            AjaxResult.error("不能删除在租资产！");
        }
        return toAjax(deleteAssetIsOk);
    }
//	@DeleteMapping("/{assetIds}")
//    @ApiOperation("删除资产台账管理")
//    public AjaxResult remove(@PathVariable Long[] assetIds)
//    {
//        return toAjax(acAssetInfoService.deleteAcAssetInfoByAssetIds(assetIds));
//    }

    // **********************************************************************

    /**
     * 复制和更新资产信息
     *
     * **/
    @PostMapping("/copyAssetInfo")
    @ApiOperation("复制和更新资产信息")
    public AjaxResult copyAcAssetInfo(@RequestBody AcAssetInfo copyAcAssetInfoPOJO) {
        int result = acAssetInfoService.copyAndUpdateAssetInfo(copyAcAssetInfoPOJO);
        // 自定义编号重复
        if(0 == result) {
            return AjaxResult.error("自定义编号重复！");
        }
        return toAjax(result);
    }

    /**
     * 校验和新增资产信息
     * **/
    @PostMapping("/checkAndInsertAssetInfo")
    @ApiOperation("校验和新增资产信息")
    public AjaxResult checkAndInsertAssetInfo(@RequestBody AcAssetInfo acAssetInfo) {
        int result = acAssetInfoService.checkAndInsertAssetInfo(acAssetInfo);
        // 自定义编号重复
        if(0 == result) {
            return AjaxResult.error("自定义编号重复！");
        }
        return toAjax(result);
    }

    @GetMapping("/selectAssetInfo/{assetId}")
    @ApiOperation("查询资产基本信息")
    public AjaxResult selectAssetInfoByAssetId(@PathVariable("assetId") Long assetId) {
        return success(acAssetInfoService.selectAssetInfoByAssetId(assetId));
    }

    @GetMapping("/selectAssetLeaseInfo/{assetId}")
    @ApiOperation("查询资产在租信息")
    public AjaxResult selectAssetLeaseInfoByAssetId(@PathVariable("assetId") Long assetId) {
        AcAssetLeaseInfoVO acAssetLeaseInfoVO = acAssetInfoService.selectAssetLeaseInfoByAssetId(assetId);
        if(null == acAssetLeaseInfoVO) {
            return success(new HashMap<>());
        }
        return success(acAssetLeaseInfoVO);
    }

    @GetMapping("/selectAssetFixByAssetId/{assetId}")
    @ApiOperation("查询保修维护工单信息")
    public AjaxResult selectAssetFixByAssetId(@PathVariable("assetId") Long assetId) {
        List<AcWorkOrder> ccWorkOrderList = acAssetInfoService.selectAssetFixByAssetId(assetId);
        if(null == ccWorkOrderList) {
            return success(new ArrayList<>());
        }
        return success(ccWorkOrderList);
    }



    @GetMapping("/selectAssetCheckByAssetId/{assetId}")
    @ApiOperation("查询保修维护工单信息")
    public AjaxResult selectAssetCheckByAssetId(@PathVariable("assetId") Long assetId) {
        List<AcWorkOrder> ccWorkOrderList = acAssetInfoService.selectAssetCheckByAssetId(assetId);
        if(null == ccWorkOrderList) {
            return success(new ArrayList<>());
        }
        return success(ccWorkOrderList);
    }



    @GetMapping("/selectAssetHistoryLeaseInfo/{assetId}")
    @ApiOperation("查询资产历史租赁信息")
    public AjaxResult selectAssetHistoryLeaseInfoByAssetId(@PathVariable("assetId") Long assetId) {
        List<AcAssetLeaseInfoVO> acAssetLeaseInfoVOList = acAssetInfoService.selectAssetHistoryLeaseInfoByAssetId(assetId);
        if(null == acAssetLeaseInfoVOList) {
            return success(new ArrayList<>());
        }
        return success(acAssetLeaseInfoVOList);
    }

    @GetMapping("/selectcAssetInfoList")
    @ApiOperation("不分页查询资产信息")
    public AjaxResult selectcAssetInfoList(AcAssetInfo acAssetInfo) {
        List<AcAssetInfo> list = acAssetInfoService.selectAcAssetInfoList(acAssetInfo);
        return AjaxResult.success(list);
    }

    @GetMapping("/selectcAssetInfoIoc")
    @ApiOperation("不分页查询资产信息")
    public AjaxResult selectAcAssetInfoIoc(AcAssetInfo acAssetInfo) {
        List<AcAssetInfo> list = acAssetInfoService.selectAcAssetInfoIoc(acAssetInfo);
        return AjaxResult.success(list);
    }


    @GetMapping("/selectAssetInfoToInsertLeaseInfo")
    @ApiOperation("查询闲置资产")
    public AjaxResult selectAssetInfoToInsertLeaseInfo(AcAssetInfo acAssetInfo) {
        acAssetInfo.setAssetStatus("0");
        List<AcAssetInfo> list = acAssetInfoService.selectAcAssetInfoList(acAssetInfo);
        return AjaxResult.success(list);
    }

}
